package com.longdaji.decoration.ui.webview;

import android.text.TextUtils;
import android.util.Log;

import com.longdaji.decoration.base.RxPresenter;
import com.longdaji.decoration.data.DataManager;
import com.longdaji.decoration.data.bean.AssessUrlBean;
import com.longdaji.decoration.data.network.CommonSubscriber;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by longtao on 2017/12/19.
 */

public class WebViewPresenter extends RxPresenter<WebViewContract.View> implements WebViewContract.Presenter {
    private final static String TAG = WebViewPresenter.class.getSimpleName();
    private DataManager mDataManager;

    @Inject
    public WebViewPresenter(DataManager dataManager) {
        this.mDataManager = dataManager;
    }


    @Override
    public void getUrl(String userId, int authType) {
        addSubscribe(mDataManager.getAssessTypeUrl(userId, authType)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<AssessUrlBean>() {
                    @Override
                    public void onSuccess(AssessUrlBean assessUrlBean) {
                        Log.d(TAG, "AssessUrlBean is" + assessUrlBean);
                        if (TextUtils.equals("true", assessUrlBean.getSuccess())) {
                            if (mView != null) {
                                mView.loadWebViewUrl(assessUrlBean.getData().getUrl());
                            }
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.toast(msg);
                        }
                    }
                }));
    }

}
